package com.itheima.reggie.handler;

import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.common.ResultInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

@RestControllerAdvice
@ResponseBody
@Slf4j
//配置异常处理器
public class GlobalExceptionHandler {
    @ExceptionHandler(DuplicateKeyException.class)
    public ResultInfo handlerException(DuplicateKeyException e){
        //输出异常信息
        log.info(e.getMessage());
        return ResultInfo.error("名字重复");
    }
    //自己定义的异常
    @ExceptionHandler(CustomException.class)
    public  ResultInfo handlerException(CustomException e){
        return ResultInfo.error(e.getMessage());
    }
    //其他异常
    @ExceptionHandler(Exception.class)
    public  ResultInfo handlerEXception(Exception e){
        return ResultInfo.error("请您检查网络链接");
    }
}
